package chat;

import javax.swing.*;
import java.net.Socket;
import java.net.ServerSocket;
import java.io.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;
import java.awt.Container;
import java.awt.BorderLayout;

public class Client extends JFrame implements Runnable
{
	BufferedReader reader;
	PrintWriter writer;
	Socket socket;
	JTextField nameBox=new JTextField("请输入你的昵称");
	JTextField sendBox=new JTextField();	
	private final JTextArea messageView = new JTextArea();
	//JScrollPane scrollPane=new JScrollPane(messageView);
	private final JButton btnNewButton = new JButton("发送");
	private final JButton btnNewButton_1 = new JButton("关闭");
	private final JButton btnNewButton_2 = new JButton("加入聊天");
	private final JButton btnNewButton_3 = new JButton("清屏");
	public Client()
	{	
		
		super("聊天程序");
		super.setBounds(300, 200, 462, 300);
		messageView.setEditable(false);
		Container c=getContentPane();
		c.setLayout(null);
		nameBox.setBounds(0, 0, 197, 30);
		messageView.setBounds(0, 40, 434, 139);
		sendBox.setBounds(0, 189, 434, 30);
		c.add(nameBox);
		//c.add(scrollPane);
		c.add(sendBox);
		nameBox.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				nameBox.setText("");
			}
		});
		getContentPane().add(messageView);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				writer.println(nameBox.getText()+":"+sendBox.getText());
				sendBox.setText("");
			}
		});
		btnNewButton.setBounds(331, 229, 93, 23);
		
		getContentPane().add(btnNewButton);
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
			}
		});
		btnNewButton_1.setBounds(228, 229, 93, 23);
		
		getContentPane().add(btnNewButton_1);
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			
			}
		});
		btnNewButton_2.setBounds(207, 3, 93, 23);
		
		getContentPane().add(btnNewButton_2);
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				messageView.setText("");
			}
		});
		btnNewButton_3.setBounds(331, 3, 93, 23);
		
		getContentPane().add(btnNewButton_3);

		sendBox.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				
			}
		});

	}
	public void run()
	{
		while(true)
		{
			try
			{
				messageView.append(reader.readLine()+"\n");
			}
			catch(IOException ioe)
			{
			   System.err.println("发生IO异常:"+ioe);
			   ioe.printStackTrace();
			}
			
		}
	}
	public void startClient(String host,int port)
	{
		try
		{
			messageView.append("连接服务器...\n");
			socket =new Socket(host,port);
			messageView.append("连接成功，您可以聊天了\n");
			reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
			writer=new PrintWriter(socket.getOutputStream(),true);
			new Thread(this).start();
		}
		catch(Exception e)
		{
			messageView.append("连接失败\n");
		}
	}
	public static void main(String[] args)
	{
		Client client=new Client();
		client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		client.setSize(600,300);
		client.setVisible(true);
		if(args.length==0)
			client.startClient("192.168.201.59",5000);
		else 
			client.startClient(args[0],5000);
		
	}
}
